package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityAboutThisPassBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterIncome
import com.gexiaobao.pigeon.ui.adapter.AdapterOutcome
import com.gexiaobao.pigeon.viewmodel.OrgRaceViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2024/7/17 13:58
 * @Describe    : 关于本关
 */
class ActivityAboutThisPass : BaseActivity<OrgRaceViewModel, ActivityAboutThisPassBinding>() {

    private var mMemberId: Int = 0
    private var mPassId: Int = 0

    private var mOrgName = ""
    private var mOrgIcon = ""
    private var mOrgRaceName = ""

    private val mAdapterInCome: AdapterIncome by lazy { AdapterIncome(arrayListOf()) }
    private val mAdapterOutCome: AdapterOutcome by lazy { AdapterOutcome(arrayListOf()) }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = getString(R.string.about_this_pass)

        mMemberId = intent.getIntExtra("memberId", 0)
        mPassId = intent.getIntExtra("passId", 0)

        mOrgIcon = intent.getStringExtra("orgIcon").toString()
        mOrgName = intent.getStringExtra("orgName").toString()
        mOrgRaceName = intent.getStringExtra("orgRaceName").toString()

        mDatabind.includeOrg.tvIncludeOrgName.text = mOrgName
        mDatabind.includeOrg.tvIncludeOrgRaceName.text = mOrgRaceName
        ImageLoaderManager.loadCircleImageFall(this, mOrgIcon, mDatabind.includeOrg.ivOrgIcon, R.mipmap.profilepic_str_1)

        mViewModel.getMemberLoftPassData(mPassId, mMemberId)

        mDatabind.swipeRefreshLayout.init {
            mViewModel.getMemberLoftPassData(mPassId, mMemberId)
        }

        mDatabind.swipeRecyclerViewIn.init(LinearLayoutManager(this), mAdapterInCome)
            .addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 15f)))

        mDatabind.swipeRecyclerViewOut.init(LinearLayoutManager(this), mAdapterOutCome)
            .addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 15f)))
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /** 获取鸽主本关卡数据  */
        mViewModel.aboutPassResult.observe(this) {
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            mDatabind.data = it

            mAdapterInCome.setList(it.income)
            mAdapterInCome.notifyDataSetChanged()

            mAdapterOutCome.setList(it.outcome)
            mAdapterOutCome.notifyDataSetChanged()
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack, mDatabind.btnIncome, mDatabind.btnDisburse, mDatabind.includeOrg.ivOrgIcon) {
            when (it) {
                mDatabind.includeOrg.ivOrgIcon -> {
                    openBigImage(mDatabind.includeOrg.ivOrgIcon, mOrgIcon)
                }

                mDatabind.btnIncome -> {
                    refreshUi("1")
                }

                mDatabind.btnDisburse -> {
                    refreshUi("2")
                }

                mDatabind.includeBar.ivBack -> {
                    finish()
                }
            }
        }
    }

    private fun refreshUi(s: String) {
        when (s) {
            "1" -> {
                mDatabind.llIncome.isVisible = true
                mDatabind.swipeRecyclerViewOut.isVisible = false
                mDatabind.btnIncome.background = ContextCompat.getDrawable(this, R.mipmap.icon_income)
                mDatabind.btnDisburse.background = ContextCompat.getDrawable(this, R.mipmap.icon_disburse)
            }

            "2" -> {
                mDatabind.llIncome.isVisible = false
                mDatabind.swipeRecyclerViewOut.isVisible = true
                mDatabind.btnIncome.background = ContextCompat.getDrawable(this, R.mipmap.icon_disburse)
                mDatabind.btnDisburse.background = ContextCompat.getDrawable(this, R.mipmap.icon_income)
            }
        }
    }
}